Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

OSGi Unit Test Framework for Carbon 5

DZone's Guide to

OSGi Unit Test Framework for Carbon 5

Here's a quick OSGi unit test framework example, complete with an intro, break down of Pax-Eaxm, and how to write a unit test case.

Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

Introduction

In the current carbon kernel there is no way to write test cases in order to test the OSGi services and functions. This is purely because of the bundles needs to be installed and run on an OSGi  container. Pax- Exam [1] framework provides the underneath infrastructure to compose unit test cases which need to be run. 

How Pax-Exam Works

Pax exam starts up the OSGi framework, (which is equinox for the carbon kernel), with a minimum level of bundles which are necessary for the pax-exam operations. Then the Pax Exam user can provision their bundles to the OSGi container. Refer [2] for the provisioned bundles list [2]. This way when the Pax-Exam boots up, these provisioned bundles will be installed to the container.

How to Write Unit Test Case

Now the Pax-Exam has booted up the OSGi framework with the necessary bundles for your test environment. You have to use the @RunWith(PaxExam.class) annotation to hook Pax Exam into Testng and let it do its magic of setting up a test container with an OSGi framework and your bundles to run the tests.

[3] is a sample test case added. In the sample you can see that it is very similar to a normal testng test case and containing some annotations which relates to pax-exam. 

Using dependency injection, your test method can access the BundleContext of the probe bundle or any service obtained from the OSGi service registry.

@Inject
    private BundleContext bundleContext;

or else you can directly access the OSGi services using the following 

@Inject
    private SomeSampleOsgiService service;

If you need any special bundles other than those defined in [2], you can always override the config method in your test case class. But make sure that testng bundle must be installed.

@Configuration
    public Option[] config() {
        return options(
mavenBundle().artifactId("testng").groupId("org.testng").versionAsInProject(),
            mavenBundle("sample", "org.wso2.carbon", "1.0.0-SNAPSHOT")
            );
    }

[1].https://ops4j1.jira.com/wiki/display/PAXEXAM4/Getting+Started+with+OSGi+Tests
[2].https://github.com/wso2/carbon4-kernel/blob/c5-pax-exam-work/tests/osgi-tests/src/test/java/org/wso2/carbon/osgi/config/CarbonOSGiConfiguration.java

/*
 * Copyright 2015 WSO2, Inc. http://www.wso2.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.osgi;

import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.ops4j.pax.exam.testng.listener.PaxExam;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import javax.inject.Inject;

/**
 * Base OSGi class to test the OSGi status of the org.wso2.carbon.core bundle
 */
@Listeners(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class BaseOSGiTest {

    @Inject
    private BundleContext bundleContext;

    @Test
    public void testBundleContextStatus() {
        Assert.assertNotNull(bundleContext, "Bundle Context is null");
    }

    @Test
    public void testCarbonCoreBundleStatus() {

        Bundle coreBundle = null;
        for (Bundle bundle : bundleContext.getBundles()) {
            if (bundle.getSymbolicName().equals("org.wso2.carbon.core")) {
                coreBundle = bundle;
                break;
            }
        }
        Assert.assertNotNull(coreBundle, "Carbon Core bundle not found");
        Assert.assertEquals(coreBundle.getState(), Bundle.ACTIVE, "Carbon Core Bundle is not activated");
    }

The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

Topics:
enterprise integration ,ei ,integration ,osgi ,unit test ,carbon 5

Published at DZone with permission of Aruna Karunarathna, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}