Search in sources :

Example 6 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project feature-flags-for-osgi by amitjoy.

the class FeatureManagerProviderTest method testConfigurationEventDeleted.

@Test
public void testConfigurationEventDeleted() throws Exception {
    final FeatureManagerProvider manager = new FeatureManagerProvider();
    manager.setConfigurationAdmin(configurationAdmin);
    manager.setMetaTypeService(metaTypeService);
    manager.activate(bundleContext1);
    final MetaTypeExtender extender = manager.getExtender();
    final String[] pids = new String[] { "a" };
    final BundleEvent bundleEvent = new BundleEvent(BundleEvent.STARTED, bundle);
    when(metaTypeService.getMetaTypeInformation(bundle)).thenReturn(metaTypeInfo);
    when(metaTypeInfo.getPids()).thenReturn(pids);
    when(metaTypeInfo.getObjectClassDefinition("a", null)).thenReturn(ocd);
    when(ocd.getAttributeDefinitions(ALL)).thenReturn(new AttributeDefinition[] { ad });
    mockADWithoutDefaultValue();
    when(bundleContext1.getBundle(0)).thenReturn(systemBundle);
    when(bundle.getState()).thenReturn(ACTIVE);
    when(bundle.getBundleContext()).thenReturn(bundleContext1);
    extender.addingBundle(bundle, bundleEvent);
    Thread.sleep(1000);
    FeatureDTO feature = manager.getFeatures().collect(Collectors.toList()).get(0);
    assertEquals(FEATURE_ID, feature.id);
    assertEquals(FEATURE_DESC, feature.description);
    assertFalse(feature.isEnabled);
    feature = manager.getFeatures(FEATURE_ID).findFirst().get();
    assertEquals(FEATURE_ID, feature.id);
    assertEquals(FEATURE_DESC, feature.description);
    assertFalse(feature.isEnabled);
    feature = manager.getFeatures(FEATURE_ID).findAny().get();
    assertEquals(FEATURE_DESC, feature.description);
    assertFalse(feature.isEnabled);
    final ConfigurationEvent configEvent = new ConfigurationEvent(reference, 2, null, "a");
    manager.configurationEvent(configEvent);
    final List<FeatureDTO> newFeatures = manager.getFeatures().collect(Collectors.toList());
    assertTrue(newFeatures.isEmpty());
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) BundleEvent(org.osgi.framework.BundleEvent) FeatureDTO(com.amitinside.featureflags.dto.FeatureDTO) Test(org.junit.Test)

Example 7 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.

the class LocationTest method testLocationChangeToRegionBinding.

@Test
public void testLocationChangeToRegionBinding() throws Exception {
    final String pid = COMPONENT_NAME;
    checkConfigurationCount(pid, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Configuration config = configure(pid);
    delay();
    findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
    Bundle b2 = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
    b2.start();
    checkConfigurationCount(b2, pid, 1, -1);
    bundle.stop();
    delay();
    checkConfigurationCount(b2, pid, 1, -1);
    ConfigurationListener listener = new ConfigurationListener() {

        public void configurationEvent(ConfigurationEvent event) {
            if (event.getType() == ConfigurationEvent.CM_LOCATION_CHANGED) {
                eventReceived = true;
            }
        }
    };
    ServiceRegistration<ConfigurationListener> sr = bundleContext.registerService(ConfigurationListener.class, listener, null);
    config.setBundleLocation(REGION);
    delay();
    if (eventReceived) {
        checkConfigurationCount(b2, pid, 1, ComponentConfigurationDTO.ACTIVE);
    }
    sr.unregister();
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 8 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.

the class LocationTest method testLocationBinding.

/*
     * tests that ds does not override a dynamic (null) location binding.
     */
@Test
public void testLocationBinding() throws Exception {
    final String pid = COMPONENT_NAME;
    deleteConfig(pid);
    delay();
    checkConfigurationCount(pid, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Configuration config = configure(pid);
    delay();
    findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
    // dynamic (null) bundle location not overridden by ds, so all bundles can use the config.
    Bundle b2 = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
    b2.start();
    checkConfigurationCount(b2, pid, 1, -1);
    bundle.stop();
    delay();
    checkConfigurationCount(b2, pid, 1, -1);
    ConfigurationListener listener = new ConfigurationListener() {

        public void configurationEvent(ConfigurationEvent event) {
            if (event.getType() == ConfigurationEvent.CM_LOCATION_CHANGED) {
                eventReceived = true;
            }
        }
    };
    ServiceRegistration<ConfigurationListener> sr = bundleContext.registerService(ConfigurationListener.class, listener, null);
    config.setBundleLocation(null);
    delay();
    if (eventReceived) {
        checkConfigurationCount(b2, pid, 1, ComponentConfigurationDTO.ACTIVE);
    }
    sr.unregister();
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 9 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.

the class ConfigInstallerTest method testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted.

public void testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted() throws Exception {
    File file = File.createTempFile("test", ".config");
    try (OutputStream os = new FileOutputStream(file)) {
        os.write("key=F\"1137191584\"\n".getBytes("UTF-8"));
    }
    String pid = "test";
    Capture<Dictionary<String, Object>> propsCapture = new Capture<>();
    Dictionary<String, Object> props = new Hashtable<>();
    props.put(DirectoryWatcher.FILENAME, file.toURI().toString());
    EasyMock.expect(mockConfiguration.getProperties()).andReturn(props);
    EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
    EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
    EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
    EasyMock.expect(mockConfiguration.getPid()).andReturn(pid);
    ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
    mockConfiguration.update(EasyMock.capture(propsCapture));
    EasyMock.expectLastCall();
    EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
    ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
    ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
    ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
    assertFalse("Configuration file should be deleted", file.isFile());
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Capture(org.easymock.Capture) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 10 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.

the class ConfigInstallerTest method testUseExistingConfigAndObserveCMDeleted.

public void testUseExistingConfigAndObserveCMDeleted() throws Exception {
    String pid = "test";
    Capture<Dictionary<String, Object>> props = new Capture<>();
    EasyMock.expect(mockConfiguration.getProperties()).andReturn(null);
    EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
    EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
    EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
    ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
    mockConfiguration.update(EasyMock.capture(props));
    EasyMock.expectLastCall();
    EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
    ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
    ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
    ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Dictionary(java.util.Dictionary) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Capture(org.easymock.Capture)

Aggregations

ConfigurationEvent (org.osgi.service.cm.ConfigurationEvent)16 ConfigurationListener (org.osgi.service.cm.ConfigurationListener)8 Test (org.junit.Test)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Hashtable (java.util.Hashtable)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 FeatureDTO (com.amitinside.featureflags.dto.FeatureDTO)3 Dictionary (java.util.Dictionary)3 Capture (org.easymock.Capture)3 Bundle (org.osgi.framework.Bundle)3 BundleEvent (org.osgi.framework.BundleEvent)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 Configuration (org.osgi.service.cm.Configuration)2 FabricException (io.fabric8.api.FabricException)1 RuntimeProperties (io.fabric8.api.RuntimeProperties)1 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1