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));
}
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));
}
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);
}
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")));
}
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());
}
Aggregations