Search in sources :

Example 16 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ConfigurationAdminTest method testDelete.

@Test
public void testDelete() throws Exception {
    org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
    String pid = "org.apache.aries.jmx.mock";
    Configuration config = mock(Configuration.class);
    when(admin.getConfiguration(pid, null)).thenReturn(config);
    ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
    mbean.delete(pid);
    verify(config).delete();
    reset(config);
    when(admin.getConfiguration(pid, "location")).thenReturn(config);
    mbean.deleteForLocation(pid, "location");
    verify(config).delete();
}
Also used : Configuration(org.osgi.service.cm.Configuration) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 17 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ConfigurationAdminTest method testGetProperties.

@Test
public void testGetProperties() throws Exception {
    org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
    String pid = "org.apache.aries.jmx.mock";
    Configuration config = mock(Configuration.class);
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("one", "value");
    props.put("two", 2);
    when(admin.getConfiguration(eq(pid), anyString())).thenReturn(config);
    when(config.getProperties()).thenReturn(props);
    ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
    TabularData properties = mbean.getPropertiesForLocation(pid, null);
    assertNotNull(properties);
    assertEquals(PROPERTIES_TYPE, properties.getTabularType());
    assertEquals(2, properties.values().size());
    PropertyData<? extends Object> oneData = PropertyData.from(properties.get(new Object[] { "one" }));
    assertEquals("value", oneData.getValue());
    PropertyData<? extends Object> twoData = PropertyData.from(properties.get(new Object[] { "two" }));
    assertEquals(2, twoData.getValue());
    assertEquals("2", twoData.getEncodedValue());
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Matchers.anyString(org.mockito.Matchers.anyString) TabularData(javax.management.openmbean.TabularData) Test(org.junit.Test)

Example 18 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ConfigurationAdminTest method testSetBundleLocation.

@Test
public void testSetBundleLocation() throws Exception {
    org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
    String pid = "org.apache.aries.jmx.mock";
    Configuration config = mock(Configuration.class);
    when(admin.getConfiguration(pid, null)).thenReturn(config);
    ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
    mbean.setBundleLocation(pid, "file:/newlocation");
    ArgumentCaptor<String> locationArgument = ArgumentCaptor.forClass(String.class);
    verify(config).setBundleLocation(locationArgument.capture());
    assertEquals("file:/newlocation", locationArgument.getValue());
}
Also used : Configuration(org.osgi.service.cm.Configuration) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 19 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ConfigurationAdminTest method testGetConfigurations.

@Test
public void testGetConfigurations() throws Exception {
    org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
    String factoryPid = "org.apache.aries.jmx.factory.mock";
    String filter = "(" + org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID + "=org.apache.aries.jmx.factory.mock)";
    String location = "../location";
    Configuration a = mock(Configuration.class);
    when(a.getPid()).thenReturn(factoryPid + "-2160133952674-0");
    when(a.getBundleLocation()).thenReturn(location);
    Configuration b = mock(Configuration.class);
    when(b.getPid()).thenReturn(factoryPid + "-1260133982371-1");
    when(b.getBundleLocation()).thenReturn(location);
    when(admin.listConfigurations(filter)).thenReturn(new Configuration[] { a, b });
    ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
    String[][] result = mbean.getConfigurations(filter);
    assertEquals(2, result.length);
    assertArrayEquals(new String[] { factoryPid + "-2160133952674-0", location }, result[0]);
    assertArrayEquals(new String[] { factoryPid + "-1260133982371-1", location }, result[1]);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 20 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ComponentTest method testComponent.

@Test
public void testComponent() throws IOException {
    OSGi<?> program = configurations("org.components.MyComponent").flatMap(props -> services(Service.class).flatMap(ms -> just(new Component(props, ms)).flatMap(component -> register(Component.class, component, new HashMap<>()).distribute(ign -> dynamic(highestService(ServiceOptional.class), component::setOptional, c -> component.setOptional(null)), ign -> dynamic(services(ServiceForList.class), component::addService, component::removeService)))));
    ServiceTracker<Component, Component> serviceTracker = new ServiceTracker<>(_bundleContext, Component.class, null);
    serviceTracker.open();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    _bundleContext.registerService(ManagedService.class, dictionary -> countDownLatch.countDown(), new Hashtable<String, Object>() {

        {
            put("service.pid", "org.components.MyComponent");
        }
    });
    Configuration factoryConfiguration = null;
    try (OSGiResult<?> run = program.run(_bundleContext)) {
        factoryConfiguration = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
        factoryConfiguration.update(new Hashtable<>());
        countDownLatch.await(10, TimeUnit.SECONDS);
        assertNull(serviceTracker.getService());
        ServiceRegistration<Service> serviceRegistration = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
        Component component = serviceTracker.waitForService(10 * 1000);
        assertNotNull(component);
        assertNull(component.getOptional());
        ServiceRegistration<ServiceOptional> serviceRegistration2 = _bundleContext.registerService(ServiceOptional.class, new ServiceOptional(), new Hashtable<>());
        Thread.sleep(1000L);
        assertNotNull(component.getOptional());
        ServiceOptional serviceOptional = new ServiceOptional();
        ServiceRegistration<ServiceOptional> serviceRegistration3 = _bundleContext.registerService(ServiceOptional.class, serviceOptional, new Hashtable<String, Object>() {

            {
                put("service.ranking", 1);
            }
        });
        assertEquals(serviceOptional, component.getOptional());
        serviceRegistration3.unregister();
        assertNotNull(component.getOptional());
        serviceRegistration2.unregister();
        assertNull(component.getOptional());
        ServiceRegistration<ServiceForList> serviceRegistration4 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
        ServiceRegistration<ServiceForList> serviceRegistration5 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
        assertEquals(2, component.getServiceForLists().size());
        serviceRegistration4.unregister();
        assertEquals(1, component.getServiceForLists().size());
        serviceRegistration5.unregister();
        assertEquals(0, component.getServiceForLists().size());
        serviceRegistration.unregister();
        assertNull(serviceTracker.getService());
    } catch (IOException ioe) {
    } catch (InterruptedException e) {
        Assert.fail("Timeout waiting for configuration");
    } finally {
        serviceTracker.close();
        if (factoryConfiguration != null) {
            factoryConfiguration.delete();
        }
    }
}
Also used : OSGi.configurations(org.apache.aries.osgi.functional.OSGi.configurations) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) OSGi.onClose(org.apache.aries.osgi.functional.OSGi.onClose) ArrayList(java.util.ArrayList) OSGi.register(org.apache.aries.osgi.functional.OSGi.register) HighestRankingRouter.highest(org.apache.aries.osgi.functional.test.HighestRankingRouter.highest) Configuration(org.osgi.service.cm.Configuration) OSGi.apply(org.apache.aries.osgi.functional.OSGi.apply) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) OSGi.just(org.apache.aries.osgi.functional.OSGi.just) Assert.assertNull(org.junit.Assert.assertNull) OSGi.services(org.apache.aries.osgi.functional.OSGi.services) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) OSGi(org.apache.aries.osgi.functional.OSGi) ManagedService(org.osgi.service.cm.ManagedService) Assert(org.junit.Assert) OSGi.bundleContext(org.apache.aries.osgi.functional.OSGi.bundleContext) FrameworkUtil(org.osgi.framework.FrameworkUtil) OSGiResult(org.apache.aries.osgi.functional.OSGiResult) Dictionary(java.util.Dictionary) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ManagedService(org.osgi.service.cm.ManagedService) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Configuration (org.osgi.service.cm.Configuration)226 Test (org.junit.Test)85 Hashtable (java.util.Hashtable)75 IOException (java.io.IOException)55 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)49 Dictionary (java.util.Dictionary)36 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 ServiceReference (org.osgi.framework.ServiceReference)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)18 Matchers.anyString (org.mockito.Matchers.anyString)16 BundleContext (org.osgi.framework.BundleContext)15 RegistrySourceConfiguration (org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration)11 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)10 File (java.io.File)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)8 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)7