Friday, October 14, 2016

Spock data driven testing with interactions (Mocks)

You can do data driven testing with Mocks and interactions, the key is that your interaction should be in the setup: block For Example
    @Unroll
    def "createOtherField test to see if label override works #fieldName override(#fieldLabel) == #resultName"() {
        setup:
        FormFieldMapManager formFieldMapManager = Mock()
        String onlineFormId = '1'
        List<LabelValue> otherFields = []
        1*formFieldMapManager.getMappedFields(onlineFormId, false) >> [new FormFieldMap()]

        expect:
        List<LabelValue> result = ProcessorUtil.createOtherField(formFieldMapManager, onlineFormId, otherFields, fieldName, 'fieldValue')
        result[0].label == resultName

        where:
        fieldName   | fieldLabel    || resultName
        'fieldName' | null          || 'fieldName'
        'fieldName' | ''            || 'fieldName'
        ''          | null          || ''
        'fieldName' | 'OVERRIDE'    || 'OVERRIDE'
    }

Tuesday, October 11, 2016

Intellij H2 Database viewer

For some reason to be able to see you tables in the Intellij H2 DB viewer you have append the following mv_store=false in your application.yml so the connection string looks like so:
jdbc:h2:file:./devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;mv_store=false

Monday, September 12, 2016

Flatten a Map of Maps

A question came up that interested me, someone had a map that contains children of possibly lists of other maps, and they wanted to flatten all these maps to count all the values contained in this map of lists of maps. I just used a recursive closure to collect all the values of the maps and lists, flatten that into a list and then an inject (fold) to count up the values.

def collector
collector = {item -> item.collect {element -> element.value instanceof Map ? collector(element.value) : element.value}}

assert 4 == collector([a: [q:1, w:1], b: [e:1], c: 1]).flatten().inject(0) { temp, val -> temp + val.value}
assert 3 == collector([a: [q:1, w:[z:1, x:1]]]).flatten().inject(0) { temp, val -> temp + val.value}

Sunday, September 11, 2016

Grails logging debug from your controller logback.groovy

To get your controllers and services logging debug messages edit your logback.groovy. Just add your package structure, for instance if your package structure was com.myapp

root(ERROR, ['STDOUT']) // Sets log level of all
logger('grails.app.controllers.com.myapp', DEBUG, ['STDOUT'], false) // Turn on debug for all your controllers
logger('grails.app.services.com.myapp', DEBUG, ['STDOUT'], false) // Turn on debug for all your services

Tuesday, June 28, 2016

Rest API Docs Swagger

I had a lot of problems trying to get swagger to work, finally found this resource which sort of worked for me.
First I started getting

Error creating bean with name 'com.github.rahulsom.swaggydoc.ApiController': Injection of autowired dependencies failed;
This was fixed by adding the following to depenedencies
compile "org.grails:grails-dependencies"

The test UserController, User domain worked. But when I added a very simple class of my won it would render on the http://localhost:8080/api page but clicking it did not bring down it operations.

Okay found the reason. For Swaggy to work you need a few crucial things:
  • Your controller needs the @Api annotation

@Api(value = 'ApiFile', description = 'Anything')
class ApiFileTypeController extends RestfulController {
    static responseFormats = ['json', 'xml']
    ApiFileTypeController() {
        super(ApiFileType)
    }
}
  • Your UrlMapping needs the url for the resource

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }
        "/filetypes"(resource: 'apiFileType')

        "/"(view:"/index")
        "500"(view:'/error')
        "404"(view:'/notFound')
    }
}

Monday, June 27, 2016

Sunday, June 26, 2016

Grails3 Twitter Bootstrap installing a theme

So Grails 3 and twitter boostrap, where is the bootstrap plugin and how do I install a theme?
First there is no plugin (well there are some plugins that seem to modify your scaffoldin, fields plugin) but Grails 3 has bootstrap as standard really.

But you have bought a theme like Material and want to get it into your app.

Get Theme

So download the Material theme unpack it and run the setup (npm install).
Lets say we want to install the jQuery light theme your theme directory should have a structure similar to:
 --Export
     |
     +--AngularJs
     |
     +--Documenation
     |
     +--jQuery
          |
          +--dark
          |
          +--light
               |
               +--css
               |
               +--fonts
               |
               +--img
               |
               +--js
               |
               +--less
               |
               +--media
               |
               +--node_modules
               |
               +--vendors
               |
               Loads html

Copy the entire light directory into your grails directory at the assets location [app]/grails-app/assets
Rename it to material.

If you run the app, you should be able to access your theme material, with the urls like
http://localhost:8080/assets/index.html
http://localhost:8080/assets/css/demo.css

Material.gsp

Now take a material theme html page, like index.html and copy it into your [app]grails-app/views/layouts directory calling it something like material.gsp.

Now lets edit the material.gsp to take out content and render our grails app in there. Find the


<section id="content">

And replace all the content in it so it looks like this. Basically nothing but including a body

<section id="content">
     <g:layoutBody/>
</section>

Replace all links to css and js includes directories to point to the new assets directory in material.gsp so these links

<link href="vendors/bower_components/fullcalendar/dist/fullcalendar.min.css" rel="stylesheet">
<script src="vendors/bower_components/jquery/dist/jquery.min.js"></script>

Become these

<link href="/assets/vendors/bower_components/fullcalendar/dist/fullcalendar.min.css" rel="stylesheet">
<script src="/assets/vendors/bower_components/jquery/dist/jquery.min.js"></script>

Use

Now we just need tell our page to use this new layout so go to your index.gsp and make the head look as follows (change the content)
<head>
    <meta name="layout" content="material"/>
Now your index.gsp page should render in this new theme

Next Steps

This just quickly shows you how to get a theme in, you still need to change the navigation and add whatever content you want, but it is a start