Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

As an example, we can see how opennaas-router and opennaas-junos features are included to run the tests of this class.

 

Writing our test class

Dependency Injection

Pax-Exam allows the injection of dependencies inside the test class directly from the OSGI service registrry. It provides two ways to do it: either using the javax.inject.Inject annotation or optional @Filter one using the BundleContext API.

Code Block
themeEclipse
languagejava
linenumberstrue
@Inject
protected IResourceManager		resourceManager;
 
@Inject
protected IProtocolManager		protocolManager;


@Inject
protected BundleContext			bundleContext;


@Inject
@Filter("(osgi.blueprint.container.symbolicname=org.opennaas.extensions.protocols.netconf)")
private BlueprintContainer		netconfService;


@Inject
@Filter("(osgi.blueprint.container.symbolicname=org.opennaas.extensions.router.repository)")
private BlueprintContainer		routerRepoService;

The example above illustrates both possibilities. First of all we inject the ResourceManager, the ProtocolManager and the BundleContext using the java.inject.Inject annotation. On the other hand, using the @Filter Pax-Exam looks in the OSGI service registry the service identified by the given type and also the given attribute value. RouterRepository service and Netconf service are retrieved using the name of the its blueprint container.

From this moment, we can call all exported methods by the OSGI services we have injected as an usual insantiated class

Code Block
themeEclipse
languagejava
linenumberstrue
@Test
public void resourceManagerIsEmpty() {
	Assert.assertTrue(resourceManager.listResources().isEmpty());
}

 

Creating tests

A simple class can contain multiple test. Tests are no more than methods annotated with the @Test annotation. After configurint and starting the OSGI container, Pax-Exam will scan the class looking for all the annotated functions and will run them according to the reactor strategy we have set. 

Code Block
themeEclipse
languagejava
linenumberstrue
/**
 * Test to check if capability is available from OSGi.
 */
@Test
public void isCapabilityAccessibleFromResource()
			throws ResourceException, ProtocolException
{
	startResource();
	Assert.assertTrue(routerResource.getCapabilities().size() > 0);

	stopResource();
	Assert.assertTrue(resourceManager.listResources().isEmpty());
}

The test checks if after starting a resource, it contains at least one capability (startResource() adds the OSPF and Queue capabilities) and that after stopping the resource it has been successfully removed from the ResourceManager service.

As in unitary tests, you can take advantage of the @Before and @After annotation. Functions with these annotations will be called before/after executing the integration test in order to prepare the test environment or to check the state of the container after them. 

 

Maven depenencies

Integration tests in OpenNaaS are located under /itests/ folder. Therefore they inherit from it all the necessary dependencies for starting and configuring the OSGI container.

Code Block
themeEclipse
languagehtml/xml
linenumberstrue
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
	</dependency>
	<dependency>
		<groupId>org.ops4j.pax.swissbox</groupId>
		<artifactId>pax-swissbox-framework</artifactId>
	</dependency>
	<dependency>
		<groupId>org.osgi</groupId>
		<artifactId>org.osgi.compendium</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>platform</artifactId>
		<version>${project.version}</version>
		<type>zip</type>
		<scope>test</scope>
		<exclusions>
			<!-- This exclusion is recommended by the Karaf test
			     container manual to ensure compatibility with
			     Eclipse. -->
			<exclusion>
				<groupId>org.apache.karaf.shell</groupId>
				<artifactId>org.apache.karaf.shell.dev</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
</dependencies>

So, the only dependencies we have to define in our pom are the ones containing the componentes we want to check. OSGF integration tests require OpenNaaS core , due it contains ResourceManager and ResourceRepository; the Router Repository to add and remove routers, the Router Model to make assertions over it, and also the capabilities and actionsets to test the OSPF developed funcionality.

Code Block
themeEclipse
languagehtml/xml
linenumberstrue
<dependencies>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>org.opennaas.core.resources</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>org.opennaas.extensions.router.repository</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>org.opennaas.extensions.router.model</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>org.opennaas.extensions.queuemanager</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>org.opennaas.extensions.router.capability</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas</groupId>
		<artifactId>org.opennaas.extensions.router.actionsets.junos</artifactId>
	</dependency>
	<dependency>
		<groupId>org.opennaas.itests</groupId>
		<artifactId>org.opennaas.itests.helpers</artifactId>
	</dependency>		
	<dependency>
		<groupId>net.i2cat.netconf</groupId>
		<artifactId>netconf4j</artifactId>
	</dependency>
</dependencies>

 

Pax-Exam Containers