Search in sources :

Example 11 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServicesNotContainsKey.

/**
 * Tests the {@link ApplicationServiceBean#getServices(String)} method for the case where the
 * services do not have the "configurations" key
 *
 * @throws Exception
 */
@Test
public void testGetServicesNotContainsKey() throws Exception {
    ApplicationServiceBean serviceBean = newApplicationServiceBean();
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    List<Service> services = new ArrayList<>();
    Service testService2 = mock(Service.class);
    Service testService1 = mock(Service.class);
    services.add(testService1);
    services.add(testService2);
    when(testService1.get("factory")).thenReturn(true);
    when(testService2.get("factory")).thenReturn(false);
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should not find any services.", serviceBean.getServices(TEST_APP_NAME), is(ListUtils.EMPTY_LIST));
}
Also used : BundleInfo(org.apache.karaf.features.BundleInfo) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ConfigurationAdminImplTest method testListServicesInvalidSyntaxException.

/**
 * Tests the {@link ConfigurationAdminImpl#listServices(String, String)} method for the case where
 * configurationAdmin.listConfigurations(....) throws an InvalidSyntaxException
 *
 * @throws Exception
 */
@Test
public void testListServicesInvalidSyntaxException() throws Exception {
    setUpListServices();
    setUpTestConfig();
    doThrow(new InvalidSyntaxException("", "")).when(testConfigAdmin).listConfigurations(anyString());
    List<Service> result = configurationAdminImpl.listServices(TEST_FACT_FILTER, TEST_FILTER);
    assertThat("Should recover gracefully but not add to the given data.", (String) result.get(0).get("name"), is(TEST_OCD));
    assertThat("Should only contain one map.", result.size(), is(1));
}
Also used : Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ManagedService(org.osgi.service.cm.ManagedService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Test(org.junit.Test)

Example 13 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ConfigurationAdminImplTest method testListServicesExistingFilter.

/**
 * Tests the {@link ConfigurationAdminImpl#listServices(String, String)} method for the case where
 * there is a filter returned by the bundleContext
 *
 * @throws Exception
 */
@Test
public void testListServicesExistingFilter() throws Exception {
    setUpTestConfig();
    setUpListServices();
    Filter testFilter = mock(Filter.class);
    List<ConfigurationAdminPlugin> testPluginList = new ArrayList<>();
    ConfigurationAdminPlugin testPlugin = mock(ConfigurationAdminPlugin.class);
    testPluginList.add(testPlugin);
    Map<String, Object> testConfigData = new HashMap<>();
    testConfigData.put(TEST_KEY, TEST_VALUE);
    when(testPlugin.getConfigurationData(anyString(), any(Map.class), any(BundleContext.class))).thenReturn(testConfigData);
    when(testFilter.match(any(Hashtable.class))).thenReturn(true);
    when(testBundleContext.createFilter(anyString())).thenReturn(testFilter);
    configurationAdminImpl.setConfigurationAdminPluginList(testPluginList);
    List<Service> result = configurationAdminImpl.listServices(TEST_FACT_FILTER, TEST_FILTER);
    assertThat("Should return the correct services.", (String) result.get(0).get("id"), is(TEST_PID));
    verify(testConfigAdmin, atLeastOnce()).listConfigurations(LIST_CONFIG_STRING);
}
Also used : HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ManagedService(org.osgi.service.cm.ManagedService) Mockito.anyString(org.mockito.Mockito.anyString) Filter(org.osgi.framework.Filter) ConfigurationAdminPlugin(org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin) HashMap(java.util.HashMap) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 14 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ConfigurationAdminImplTest method testListServicesPasswordType.

/**
 * Tests the {@link ConfigurationAdminImpl#listServices(String, String)} method and connected
 * methods for the case where the Configuration contains a password property and that its value is
 * properly changed to "password".
 *
 * @throws Exception
 */
@Test
public void testListServicesPasswordType() throws Exception {
    setUpTestConfig();
    setUpListServices();
    // Create a password property with a value of "secret".
    Dictionary<String, Object> testProp = new Hashtable<>();
    testProp.put("password", "secret");
    when(testAttDef.getID()).thenReturn("password");
    when(testAttDef.getType()).thenReturn(AttributeDefinition.PASSWORD);
    when(testConfig.getProperties()).thenReturn(testProp);
    List<Service> result = configurationAdminImpl.listServices(TEST_FACT_FILTER, TEST_FILTER);
    // Assert that the password value was changed from "secret" to "password".
    String password = (String) ((Map<String, Object>) ((List<Map<String, Object>>) result.get(0).get("configurations")).get(0).get("properties")).get("password");
    assertThat(password, is(equalTo("password")));
}
Also used : Hashtable(java.util.Hashtable) Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ManagedService(org.osgi.service.cm.ManagedService) Mockito.anyString(org.mockito.Mockito.anyString) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ConfigurationAdminImpl method getServices.

private List<Service> getServices(String serviceClass, String serviceFilter, boolean ocdRequired) throws InvalidSyntaxException {
    List<Service> serviceList = new ArrayList<>();
    // service.factoryPid cannot be searched, but service.pid can be searched,
    // and can contain a factoryPid
    String newFilter = null;
    if (serviceFilter != null) {
        newFilter = serviceFilter.replace("service.factoryPid", "service.pid");
    }
    // find all ManagedServiceFactories to get the factoryPIDs
    ServiceReference[] refs = this.getBundleContext().getAllServiceReferences(serviceClass, newFilter);
    for (int i = 0; refs != null && i < refs.length; i++) {
        Object pidObject = refs[i].getProperty(SERVICE_PID);
        // only include valid PIDs
        if (pidObject instanceof String && isAllowedPid((String) pidObject)) {
            String pid = (String) pidObject;
            String name = pid;
            boolean haveOcd = !ocdRequired;
            final ObjectClassDefinition ocd = getObjectClassDefinition(refs[i].getBundle(), pid);
            if (ocd != null) {
                name = ocd.getName();
                haveOcd = true;
            }
            if (haveOcd && ocd != null) {
                Service service = new ServiceImpl();
                String description = ocd.getDescription();
                service.setId(pid);
                if (StringUtils.isNotEmpty(description)) {
                    service.setDescription(description);
                }
                service.setName(name);
                serviceList.add(service);
            }
        }
    }
    return serviceList.stream().filter(service -> isPermittedToViewService(service.getId())).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) Constants(org.osgi.framework.Constants) Enumeration(java.util.Enumeration) LoggerFactory(org.slf4j.LoggerFactory) ConfigurationStatus(org.codice.ddf.admin.core.api.ConfigurationStatus) LogSanitizer(org.codice.ddf.log.sanitizer.LogSanitizer) Locale(java.util.Locale) MetatypeAttribute(org.codice.ddf.admin.core.api.MetatypeAttribute) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference) Service(org.codice.ddf.admin.core.api.Service) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Metatype(org.codice.ddf.admin.core.api.Metatype) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ConfigurationAdminPlugin(org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin) BundleContext(org.osgi.framework.BundleContext) ConfigurationDetails(org.codice.ddf.admin.core.api.ConfigurationDetails) Permissions(ddf.security.permission.Permissions) List(java.util.List) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) SecurityUtils(org.apache.shiro.SecurityUtils) ConfigurationProperties(org.codice.ddf.admin.core.api.ConfigurationProperties) Dictionary(java.util.Dictionary) SERVICE_PID(org.osgi.framework.Constants.SERVICE_PID) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) SERVICE_FACTORYPID(org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID) Configuration(org.osgi.service.cm.Configuration) Subject(org.apache.shiro.subject.Subject) Hashtable(java.util.Hashtable) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Logger(org.slf4j.Logger) IOException(java.io.IOException) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ManagedService(org.osgi.service.cm.ManagedService) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) ArrayList(java.util.ArrayList) Service(org.codice.ddf.admin.core.api.Service) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ManagedService(org.osgi.service.cm.ManagedService) ServiceReference(org.osgi.framework.ServiceReference) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Aggregations

Service (org.codice.ddf.admin.core.api.Service)18 ArrayList (java.util.ArrayList)14 MetaTypeService (org.osgi.service.metatype.MetaTypeService)14 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)8 IOException (java.io.IOException)6 List (java.util.List)6 ManagedService (org.osgi.service.cm.ManagedService)6 Collections (java.util.Collections)5 Hashtable (java.util.Hashtable)5 Collectors (java.util.stream.Collectors)5 Arrays (java.util.Arrays)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SystemService (org.apache.karaf.system.SystemService)4 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)4 ConfigurationAdminPlugin (org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin)4 BundleContext (org.osgi.framework.BundleContext)4 Filter (org.osgi.framework.Filter)4