Running HTTP/REST Integration tests in an Eclipse Tycho Build

A few days ago my fellow Frank wrote about running HTTP/REST Integration Tests efficiently in Eclipse. Today I want to show you how to embed those tests in a headless build using Eclipse Tycho. Embedding restfuse in a build is as simple as embedding plain JUnit tests. But when trying to start an OSGi server and test against this server as Frank described things are little bit more complicated.

Our scenario is identical to Frank’s. We publish REST Services within an OSGi instance using the OSGi-JAX-RS Connector. This is optional – you can publish the services any way you prefer. Our requirements are that before the tests run, an OSGi instance needs to start, the services should be published and the tests should run against the services afterwards. Using Tycho this is basically an easy task because Tycho has the eclipse-test-plugin packaging type doing most of the required things.

To follow the steps below, you can download the source from github: https://github.com/eclipsesource/restfuse-tycho-integration

In our scenario we have a bundle called com.eclipsesource.restfuse.example.service. This bundle publishes a REST service using the OSGI-JAX-RS Connector by registering @Path annotated services. Our restfuse integration tests are located in the com.eclipsesource.restfuse.example.test bundle (not a fragment in this case, but it doesn’t really matter). The parent pom of our build sits in the project com.eclipsesource.restfuse.example.build. The parent pom has one section to note which is the repositories section. In order to run restfuse tests, the restfuse p2 repository needs to be added here.

<repositories>
  <repository>
    <id>juno</id>
    <layout>p2</layout>
    <url>http://download.eclipse.org/releases/juno</url>
  </repository>
  <repository>
    <id>osgi-jaxrs</id>
    <layout>p2</layout>
    <url>http://hstaudacher.github.com/osgi-jax-rs-connector</url>
  </repository>
  <repository>
    <id>restfuse</id>
    <layout>p2</layout>
    <url>http://download.eclipsesource.com/technology/restfuse/p2</url>
  </repository>
</repositories>

The most interesting part is in the build section of the test plugin pom file. Here the OSGi environment needs to be specified. The eclipse-test-plugin packaging type can resolve bundle dependencies automatically. But when using implicit dependencies you need to put a little more work into the build section. In our example we have three implicit dependencies. These are dependencies to org.eclipse.equinox.ds, com.eclipsesource.osgi.jaxrs.connector and to the Jetty HttpService implementation.

Another “not so” obvious dependency exists in our com.eclipsesource.restfuse.example.test bundle. Because we are writing integration tests to test the HTTP interface, we don’t have any hard dependency on the code sitting in the application bundle (com.eclipsesource.restfuse.example.service). Nevertheless, we need to add a dependency to this bundle to enable Tycho to resolve it for the tests.

After the dependencies, we need to configure the OSGi instance. This means we need to start bundles, set start levels and set properties. In our example we need to start the bundles we have implicit dependencies on. Next, we need to start a fourth bundle explicitly, that is, our application bundle. The property we need to set is the port for the HttpService (org.osgi.service.http.port). As Frank described in his blog post, this property will be also used to get the port for the Destination @Rule. The full build section of the test plugin looks like this:

<build>
  <plugins>
    <plugin>
      <groupId>${tycho-groupid}</groupId>
      <artifactId>tycho-surefire-plugin</artifactId>
      <version>${tycho-version}</version>
      <configuration>
        <argLine>-Dorg.osgi.service.http.port=11042</argLine>
        <showEclipseLog>true</showEclipseLog>
        <bundleStartLevel>
          <bundle>
            <id>org.eclipse.equinox.ds</id>
            <level>1</level>
            <autoStart>true</autoStart>
          </bundle>
          <bundle>
            <id>org.eclipse.equinox.http.jetty</id>
            <level>2</level>
            <autoStart>true</autoStart>
          </bundle>
          <bundle>
            <id>com.eclipsesource.jaxrs.connector</id>
            <level>3</level>
            <autoStart>true</autoStart>
          </bundle>
          <!-- Start bundles that contribute REST services -->
          <bundle>
            <id>com.eclipsesource.restfuse.example.service</id>
            <level>4</level>
            <autoStart>true</autoStart>
          </bundle>
        </bundleStartLevel>
        <dependencies>
          <dependency>
            <type>p2-installable-unit</type>
            <artifactId>org.eclipse.equinox.ds</artifactId>
            <version>1.4.0</version>
          </dependency>
          <dependency>
            <type>eclipse-feature</type>
            <artifactId>org.eclipse.equinox.core.feature</artifactId>
            <version>1.1.0</version>
          </dependency>
          <dependency>
            <type>eclipse-feature</type>
            <artifactId>org.eclipse.equinox.compendium.sdk</artifactId>
            <version>3.8.0</version>
          </dependency>
          <dependency>
            <type>eclipse-feature</type>
            <artifactId>org.eclipse.equinox.server.jetty</artifactId>
            <version>1.1.0</version>
          </dependency>
          <dependency>
            <type>p2-installable-unit</type>
            <artifactId>com.eclipsesource.jaxrs.connector</artifactId>
            <version>2.0.0</version>
          </dependency>
        </dependencies>
      </configuration>
    </plugin>
  </plugins>
</build>

You’ll notice that in addition to the implicit dependencies we also added dependencies to org.eclipse.equinox.core.featureorg.eclipse.equinox.compendium.sdk and org.eclipse.equinox.server.jetty to satisfy the runtime dependencies.

To launch the build you can use the Eclipse m2e tooling using the launch config located in com.eclipsesource.restfuse.example.build or simply run mvn clean verify from the same directory. After the tests complete the test reports will be in com.eclipsesource.restfuse.example.test/target/surefire-reports.

reports Running HTTP/REST Integration tests in an Eclipse Tycho Build

It took me a while to figure out this setup and I hope this blog will save you a little time. If you have setups for other build systems I would really love to see them. As always, feel free to disagree icon wink Running HTTP/REST Integration tests in an Eclipse Tycho Build

followme Running HTTP/REST Integration tests in an Eclipse Tycho Build

Comments are closed.

No responses yet

Written by . Published in Categories: EclipseSource News, Planet Eclipse, Planet OSGi

Published:
Sep 11th, 2012
Follow:

Twitter Google+ GitHub Facebook