Test a new resource

Once your resource has been developed, you can test it implementation independently , but also it’s necessary to check its behaviour with the rest of the components of OpenNaaS. Therefore you can divide your testing in unitary and integration tests.

Testing basic functionality

Unitary tests are fasts and useful to test atomics part of your code. Using them, you can assure your module works perfectly in an isolated environment. Since OpenNaaS is an OSGI application, there are limitations in the set of methods of your classes you can test. The ones requiring an interaction with the OSGI Service Registry are out of scope, since unitary tests are not executed in an OSGI container.

A short example list of functionality you may test in unitary tests is:

  • ResourceBootstrapperFactory instantiates the ResourceBoorstrapper it should.
  • Methods from capabilities not using the queue, since it requires asking to the Service Registry to get it.
  • Actions from the driver, if any.
  • Additional helpers methods or functionalities used by capabilities and/or driver.

If you take the SampleResource as reference, you will notice that the single ExampleCapability exported method (sayHello)  can be tested, since it’s self-contained and independent from OSGI, but the ones  retrieving services (getActionSet, queueAction) require to run inside the framework container.


public class ExampleCapability extends AbstractCapability implements IExampleCapability {

	public static String	CAPABILITY_TYPE	= "example";

        @Override
	public IActionSet getActionSet() throws CapabilityException {

		String name = this.descriptor.getPropertyValue(ResourceDescriptorConstants.ACTION_NAME);
		String version = this.descriptor.getPropertyValue(ResourceDescriptorConstants.ACTION_VERSION);

		try {
			return Activator.getExampleActionSetService(name, version);
		} catch (ActivatorException e) {
			throw new CapabilityException(e);
		}
	}

	/**
	 * @param the
	 *            user name
	 * @return the greeting message
	 * 
	 */
	@Override
	public String sayHello(String userName) throws CapabilityException {
		return "Hello " + userName;
	}

As seen, the getActionSet method calls the static class “Activator”, in order to get the bundle context and asks for a specific service in the Service Registry. Therefore we can only test the sayHello method, and a single test would look like these:


	@Test
	public void SampleResourceBootrapperTest() {

		SampleResourceBootstrapperFactory factory = new SampleResourceBootstrapperFactory();

		IResourceBootstrapper bootstrapper = factory.createResourceBootstrapper();
		Assert.assertNotNull(bootstrapper);
		Assert.assertTrue(bootstrapper instanceof SampleResourceBootstrapper);

	}

	@Test
	public void ExampleCapabilityTest() throws CapabilityException {
		IExampleCapability capab = new ExampleCapability(sampleDescriptor(), null);
		String greetings = capab.sayHello(name);
		Assert.assertNotNull(greetings);
		Assert.assertEquals("Hello " + name, greetings);
	}

The capability method can be tested, since it’s implementation does not require an interaction with the OSGI framework, but the capability had to be created without the properly capability factory, and neither it’s associated to a resource.

Unitary tests are developed inside the same bundle, in src/test/java folder.

Testing integration

With integration tests you can check how you new resource behaves with the rest of the different modules OpenNaaS consists of. Therefore they are launched inside an OSGI container provided by PAX-EXAM.  Pax-Exam is an automated testing framework for OSGI applications. Its main particularity is how it works. Pax-Exam starts an OSGI container, installs bundles into it, and runs our test classes. This methodology allows us to remove the limitations we had in unitary tests, so we are able to interact with the Service Registry to publish our services and asks for services provided by other modules.

Integration tests for new resource must be developed as a Maven module in /itests folder. Either you can create it from scratch or use one of our archetypes, called “itets-archetype”. You can find it in /utils/archetypes/itests-archetype folder, and it needs to be installed so that Maven can recognize it as an available archetype. The archetype contain a basic pom.xml file with the usual dependencies and a single class with the main architecture of an integration test.

There’s a list of asserts you can check in order to assure your implementation is valid:

  • All services are successfully published and can be injected in your test.
  • Your resource can be created, started, stopped and removed using the Resource Manager.
  • The capabilities contracts are fulfilled by the implementations.

If your implementation fulfills all three conditions, your new resource is a valid OpenNaaS resource and can be integrated in the platform.

As an guide, you can take a look at the SampleResource integration test. A SampleResource is instantiated including a ExampleCapability, using the Resource Manager to create it.


@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(EagerSingleStagedReactorFactory.class)
public class ExampleCapabilityTest {

	@Inject
	protected IResourceManager	resourceManager;

	private IResource			sampleResource;

	private final static String	RESOURCE_TYPE			= "sampleresource";
	private final static String	SAMPLE_CAPABILITY_TYPE	= "example";
	private final static String	CAPABILITY_IMPL_VERSION	= "1.0";
	private final static String	CAPABILITY_IMPL_NAME	= "dummy";

	@Configuration
	public static Option[] configuration() {
		return options(opennaasDistributionConfiguration(),
				includeFeatures("opennaas-core", "opennaas-sampleresource", "itests-helpers"),
				noConsole(),
				keepRuntimeFolder());
	}

	@Test
	public void sampleTest() throws CapabilityException {
		Assert.assertNotNull(sampleResource.getCapabilities());
		Assert.assertEquals(1, sampleResource.getCapabilities().size());

		ICapability capab = sampleResource.getCapabilities().get(0);
		Assert.assertTrue(capab instanceof IExampleCapability);

		IExampleCapability sampleCapability = (IExampleCapability) capab;
		String greetings = sampleCapability.sayHello("OpenNaaS");
		Assert.assertEquals("Hello OpenNaaS", greetings);
	}

	@Before
	public void prepareTest() throws ResourceException {
		startResource();
	}

	private void startResource() throws ResourceException {

		List<CapabilityDescriptor> lCapabilityDescriptors = new ArrayList<CapabilityDescriptor>();

		CapabilityDescriptor exampleCapabilityDescriptor = ResourceHelper.newCapabilityDescriptor(CAPABILITY_IMPL_NAME,
				CAPABILITY_IMPL_VERSION, SAMPLE_CAPABILITY_TYPE, "mock://user:pass@host.net:2212/mocksubsystem");
		lCapabilityDescriptors.add(exampleCapabilityDescriptor);

		ResourceDescriptor resourceDescriptor = ResourceHelper.newResourceDescriptor(lCapabilityDescriptors, RESOURCE_TYPE,
				"mock://user:pass@host.net:2212/mocksubsystem", "sample-resource");

		sampleResource = resourceManager.createResource(resourceDescriptor);
		resourceManager.startResource(sampleResource.getResourceIdentifier());

	}

	@After
	public void revertTest() throws ResourceException {
		resourceManager.stopResource(sampleResource.getResourceIdentifier());
		resourceManager.removeResource(sampleResource.getResourceIdentifier());

	}
}


This single test checks that:

  • SampleResource repository is published (indirectly tested, since Resource Manager is able to create sampleResource resources)
  • SampleResource bootstrapper is published (indirectly tested, since Resource Manager is able to start sampleResource resources)
  • ExampleCapabilityFactory is published (indirectly, by creating a resource with the “ExampleCapability” capability descriptor).
  • Resource contain the ExampleCapability capability.
  • The implementation of the ExampleCapability.sayHello method fulfills the capability contract.