In Part 1 of this series we set up a development environment for the AcmeAir application. This time we’re going to automate building and testing of the application with Liberty, Maven, and Chef, which will later allow us to implement continuous integration, a key part of any continuous deployment pipeline.

How we’ll use Liberty, Maven, and Chef

The AcmeAir application has an acmeair-itests project that contains integration tests. The integration tests exercise the REST API of the application. Since the tests require an application to be running, we will configure the project to automatically start the Liberty profile server running the application before the tests and stop the server afterwards.

The acmeair-itests project is pre-configured to create a Liberty user directory with the AcmeAir application, server configuration, and its dependencies such as the Derby database. To start and stop the Liberty profile server we will use the Liberty Maven plug-in. To prevent port conflicts, we will combine it with the Build Helper Maven plug-in to find and assign a free port to the Liberty profile server. We will use the Maven profile feature to activate the execution of the integration tests.

Enable activation of integration tests

In Part 1 we ran Maven to build AcmeAir, specifically the Maven ‘install’ goal. This isn’t yet configured to run the integration tests. We’re going make a change so the integration tests will run if the Liberty installation directory is passed to Maven in the wlpInstallDir property.

  1. Open the acmeair-itests/pom.xml file.
  2. Click on the pom.xml tab.
  3. Find and remove the XML comment marks (<!-- and -->) around the following block in pom.xml:
        <id>run-its</id>
        <activation>
            <property>
                <name>wlpInstallDir</name>
            </property>
        </activation>
    
  4. Save the changes.

When the wlpInstallDir property is specified, the run-its Maven profile will be activated. The profile will enable a few Maven plug-ins that allow the integration tests to run after the following steps.

Enable the Build Helper Maven plug-in

The Build Helper Maven plug-in is used to find an available network port. This port will be used for the HTTP endpoint of the Liberty profile server. The available port is stored under the wlp.http.port Maven property.

  1. In the same editor as the previous step, find and remove the XML comment marks (<!-- and -->) around the following block in pom.xml:
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>reserve-network-port</id>
                    <goals>
                        <goal>reserve-network-port</goal>
                    </goals>
                    <phase>process-resources</phase>
                    <configuration>
                        <portNames>
                            <portName>wlp.http.port</portName>                
                        </portNames>
                    </configuration>
                </execution>
            </executions>
        </plugin> 
    
  2. Save the changes.

Enable and configure Liberty Maven plug-in

The Liberty Maven plug-in is used to start the Liberty profile server before integration tests are executed and stop the server after the tests are done. The plug-in will pass the wlp.http.port property assigned by the Build Helper plug-in to the Liberty profile server as a bootstrap property. The plug-in is also configured to use a Liberty user directory created under the project’s target/ directory.

  1. In the same editor as in the previous step, find and remove the XML comment marks (<!-- and -->) around the following block in pom.xml:
        <plugin>
            <groupId>net.wasdev.wlp.maven.plugins</groupId>
            <artifactId>liberty-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>start-liberty-server</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start-server</goal>
                    </goals>
                    <configuration>
                        <applications>acmeair</applications>
                        <bootstrapProperties>   
                            <httpPort>${wlp.http.port}</httpPort>
                        </bootstrapProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-liberty-server</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop-server</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <serverHome>${wlpInstallDir}</serverHome>
                <userDirectory>${project.build.directory}/usr</userDirectory>
                <serverName>acmeair</serverName>
            </configuration>
        </plugin>
    
  2. Save the changes.

This change causes the Liberty profile server to be started prior to the integration tests being run and stopped just afterwards.

Enable and configure Maven Failsafe plug-in

The Maven Failsafe plug-in is used to execute the integration tests. The tests will exercise the REST API of the application. To do so, the tests must be passed the port of the Liberty profile server. The value of the wlp.http.port property assigned by the Build Helper plug-in is passed as the httpPort system property to the tests.

  1. In the same editor as before, find and remove the XML comment marks (<!-- and -->) around the following block in pom.xml:
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <systemPropertyVariables>                              
                    <httpPort>${wlp.http.port}</httpPort>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    
  2. Save the changes.

Create a Run Configuration for AcmeAir integration tests

In order to run the AcmeAir integration tests from Eclipse we need to create a new Eclipse run configuration. We will also need to pass the location of the Liberty profile installation to the Liberty Maven plug-in so it can start and stop the server.

  1. Select Run configurations… option from Run menu.
  2. Locate the Maven Build entry in the left-hand list: Right-click it and select New.
  3. Type run-itests in the Name field.
  4. Set the base directory by clicking on the Browse Workspace… button and select the acmeair project.
  5. Type install in the Goals field.
  6. Create a new parameter by clicking on the Add… button. Type in wlpInstallDir as the parameter name and your Liberty profile installation directory e.g. /home/<username>/liberty/wlp as the parameter value.
  7. Click Apply.
  8. Click the Run button to run the integration tests. The tests should complete successfully.

Examine the console output to see the Liberty Maven plug-in starting the server before the tests run. Examine the console output to see the Liberty Maven plug-in stopping the server after the tests.

View the test reports

The integration tests are located in the acmeair-itests/src/test/java/ directory. After the tests run, the test reports are generated under the acmeair-itests/target/failsafe-reports/ directory. To view the test reports:

  1. Expand the acmeair-itests project. Then expand the target directory within that.
  2. Right-click the target directory and select Refresh. The failsafe-reports directory will appear.
  3. Double-click the TEST-com.acmeair.test.AcmeAirTest.xml file to see the report.

Push the pom.xml change to Git

In preparation for Part 3 we will need to push the changes you’ve just made to the acmeair-itests pom.xml to Git so the itests can be run in a Jenkins build.

  1. In Eclipse, right click on the acmeair project and select Team > Commit….
  2. Type in Configure acmeair-itests as the commit message.
  3. Under Files ensure only the acmeair-itests/pom.xml file is selected by unchecking the acmeair-webapp/src/main/webapp/js/acmeair-common.js file.
  4. Click Commit and Push. The change is pushed to the Git repository and a results window is displayed.
  5. Click on OK.

And that’s it. We can now build and test our application on demand, with our scripts checked into source control. Next time we’ll be using Jenkins to run these whenever we commit changes. See you then!

Join The Discussion

Your email address will not be published. Required fields are marked *