Saturday, October 3, 2015

Validating XML against an XSD with xmllint

Quick reference for how to validate an XML document against a schema using xmlint:
  $ xmllint --noout --schema stats.xsd example-stat.xml 
  example-stat.xml validates
Here is what you might see for an invalid document:
  $ xmllint --noout --schema stats.xsd junk.xml
  junk.xml:1: element event: Schemas validity error : Element 'event': No matching global declaration available for the validation root.
  junk.xml fails to validate

Thursday, October 1, 2015

Groovy: Closure Currying Example

Really simple example of closure currying so I do not forget it and hope that it helps someone else:
def addTo = { Map m, String k, Object v ->
    m[k] = v
}

Map elements = [:]

addTo elements, 'a', 1

def addTo2 = addTo.curry(elements)

addTo2 'c', 3

assert elements == ['a':1, 'c':3]

Sunday, March 8, 2015

Comparing JSON Strings with Jackson

Here's a quick way to compare two JSON String using Jackson. While we could always do a simple String compare, this method allows us to keep the expected JSON in a readable format, such as pretty-printed in a test resource:

import com.fasterxml.jackson.databind.ObjectMapper

class JsonAssert {

    static ObjectMapper mapper = new ObjectMapper()

    static void areEqual(String json1, String json2) {
        def tree1 = mapper.readTree json1
        def tree2 = mapper.readTree json2

        assert tree1.equals(tree2)
    }

}

The above can be called from an expect: or then: block in a Spock specification.

Saturday, February 14, 2015

Simple Shell Script for Maven Integration Tests

Here's a simple shell script it took me forever to add to make it easier for me to run an integration test with Maven:

$ cat ~/bin/it
#!/bin/bash
mvn clean verify -Dit -Dit.test=$1
I just have to remember the test name now instead of repeatedly typing the above:
$ it MyIntegrationSpec