Swagger JAXB

JAXB XJC Plugin for automatically adding annotations from Swagger to generated classes from an XSD. I’ve created this when I noticed that for very complex schemas when there are references that stack the Swagger UI Javascript gave Stackoverflow errors, adding the annotations manually to the generated classes fixed it. But: you don’t want to change generated code huh! Thus swagger-jaxb emerged from the need to add swagger annotations automatically.

Please note this plugin is in development phase !! Currently only available with maven through the sonatype snapshot repositories. It is not complete and there are some open issues, but you can already use it.

Is all going well ?

Tests run in separate project, but do they work ? see here for the code https://github.com/redlab/swagger-jaxb-tck

How to use it?

  • build the plugin with maven and install it in your local repo or get it from an external repo like sonatypes snapshot repository
  • add the plugin to your classpath and use -swaggify on your jaxb command line or use it with jaxb2-maven-plugin
<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.5</version>
        <executions>
                <execution>
                <id>internal.generate</id>
                <goals>
                    <goal>xjc</goal>
                </goals>
                <configuration>
                    <arguments>-swaggerify</arguments>
                    <clearOutputDir>true</clearOutputDir>
                    <schemaDirectory>${project.basedir}/src/main/resources/xsd//api</schemaDirectory>
                    <packageName>com.example.api.model</packageName>
                    <staleFile>${project.build.directory}/generated-sources/jaxb/.api.internal</staleFile>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>javax.xml.parsers</groupId>
                <artifactId>jaxp-api</artifactId>
                <version>1.4.5</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.parsers</groupId>
                <artifactId>jaxp-ri</artifactId>
                <version>1.4.5</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-xjc</artifactId>
                <version>2.2.7-b53</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
                <version>2.2.7-b53</version>
            </dependency>
            <dependency>
                <groupId>be.redlab.jaxb</groupId>
                <artifactId>swagger-jaxb</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </plugin>

The plugin dependencies are needed until the JAXB2 plugin is updated to use the latest version of jaxb-xjc and jaxp. Otherwise the code generation will fail due to missing methods. Note: I think this will change the generated code for boolean getters/setters, not fully sure I must verify it to be sure 🙂

post an issue at github if you really want dev version in Maven Central.

Unit testing the database layer

Testing the database layer. Usually an important part of many applications and thus everyone should test it.

But how do you do that?
(if you know how it’s done, this post is not for you, go read something more interesting then!)

You don’t want to setup an Oracle, MySQL, PostgreSQL and such just for running unit tests. Your option? Use an in memory database!

There are a couple of tools available on the opensource market to test your dao layer. I’ve put together an example for a database layer that is using Spring 3.1.2 with JPA2/Hibernate 4.x. For testing I choose the combination of Unitils, H2 in memory database and JUnit.

Unitils is the thing that creates the database using your entities or if you want it can also create your database using an sql script. I usually let hibernate create the database. And add some <dataset> xmls with data.

You can find the example code at github.

Take a look at the tests in src/test/java to see how it is done. There is one test that uses an xml file to populate the database and another test without an xml file. I’ve added Javadoc to the tests to explain what is happening.

This way of testing also allows you to test your service layer with a predefined dataset. Personally I prefer to mock stuff in the service layers. But for doing integration tests it can be useful to have an in memory database.

Happy Testing!

Stop copying that TestSuccessException

Finally you can stop copying that TestSuccessException you write when writing tests with mocking.

I’ve put it in a jar and uploaded it to Maven Central.

<dependency>
    <groupid>be.redlab.testhelpers</groupid>
    <artifactid>testhelpers</artifactid>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

The code is on GitHub there is also an EmptyHttpServletRequest, can be useful when calling methods that need a HttpServletRequest as parameter. ( e.g. in Mockito you can’t mock interfaces, so you can mock with the EmptyHttpServletRequest )

At the time being those where the one I needed, want to add more? Just leave an issue on GitHub and/or send a pull request for interesting stuff.