use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class ApplicationServiceBeanTest method testGetServicesMetatypeInfo.
/**
* Tests the {@link ApplicationServiceBean#getServices(String)} method for the case where the
* services do not have the "configurations" key and there is MetatypeInformation present for each
* service.
*
* <p>This test mostly just checks that
*
* @throws Exception
*/
@Test
public void testGetServicesMetatypeInfo() throws Exception {
ApplicationServiceBean serviceBean = newApplicationServiceBean();
ServiceTracker testServiceTracker = mock(ServiceTracker.class);
serviceBean.setServiceTracker(testServiceTracker);
MetaTypeService testMTS = mock(MetaTypeService.class);
MetaTypeInformation testMTI = mock(MetaTypeInformation.class);
when(testServiceTracker.getService()).thenReturn(testMTS);
when(testMTS.getMetaTypeInformation(any(Bundle.class))).thenReturn(testMTI);
when(testMTI.getPids()).thenReturn(new String[] { "001", "002" });
when(testMTI.getFactoryPids()).thenReturn(new String[] { "001", "002" });
Bundle testBundle = mock(Bundle.class);
Bundle[] bundles = { testBundle };
when(bundleContext.getBundles()).thenReturn(bundles);
when(testBundle.getLocation()).thenReturn(TEST_LOCATION);
List<Service> services = new ArrayList<>();
Service testService2 = mock(Service.class);
Service testService1 = mock(Service.class);
services.add(testService1);
services.add(testService2);
List<Map<String, Object>> testService1Configs = new ArrayList<>();
Map<String, Object> testConfig1 = new HashMap<>();
testConfig1.put("bundle_location", TEST_LOCATION);
testService1Configs.add(testConfig1);
List<Map<String, Object>> testService2Configs = new ArrayList<>();
Map<String, Object> testConfig2 = new HashMap<>();
testConfig2.put("bundle_location", TEST_LOCATION);
testService1Configs.add(testConfig2);
when(testService1.get("factory")).thenReturn(true);
when(testService2.get("factory")).thenReturn(false);
when(testService1.get("configurations")).thenReturn(testService1Configs);
when(testService2.get("configurations")).thenReturn(testService2Configs);
when(testService1.get("id")).thenReturn("001");
when(testService2.get("id")).thenReturn("002");
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 find the given services.", serviceBean.getServices(TEST_APP_NAME).get(0), is(testService1));
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class ConfigurationAdminImpl method getObjectClassDefinitions.
/**
* Returns the <code>ObjectClassDefinition</code> objects for the IDs returned by the <code>
* idGetter</code>. Depending on the <code>idGetter</code> implementation this will be for factory
* PIDs or plain PIDs.
*
* @param idGetter The {@link IdGetter} used to get the list of factory PIDs or PIDs from <code>
* MetaTypeInformation</code> objects.
* @return Map of <code>ObjectClassDefinition</code> objects indexed by the PID (or factory PID)
* to which they pertain
*/
private Map<String, ObjectClassDefinition> getObjectClassDefinitions(final IdGetter idGetter, Map<Long, MetaTypeInformation> metaTypeInformationByBundle) {
final Map<String, ObjectClassDefinition> objectClassesDefinitions = new HashMap<>();
final MetaTypeService mts = this.getMetaTypeService();
if (mts == null) {
return objectClassesDefinitions;
}
final Bundle[] bundles = this.getBundleContext().getBundles();
for (Bundle bundle : bundles) {
final MetaTypeInformation mti = metaTypeInformationByBundle.computeIfAbsent(bundle.getBundleId(), id -> mts.getMetaTypeInformation(bundle));
objectClassesDefinitions.putAll(findOcdById(idGetter, mti));
}
return objectClassesDefinitions;
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class AdminConsoleServiceTest method getConfigAdmin.
private AdminConsoleService getConfigAdmin() throws IOException, InvalidSyntaxException, NotCompliantMBeanException {
final BundleContext testBundleContext = mock(BundleContext.class);
final MetaTypeService testMTS = mock(MetaTypeService.class);
ConfigurationAdminImpl configurationAdminImpl = new ConfigurationAdminImpl(CONFIGURATION_ADMIN, new ArrayList<>()) {
@Override
BundleContext getBundleContext() {
return testBundleContext;
}
@Override
MetaTypeService getMetaTypeService() {
return testMTS;
}
@Override
public boolean isPermittedToViewService(String servicePid) {
return true;
}
@Override
public boolean isPermittedToViewService(String servicePid, Subject subject) {
return true;
}
};
AdminConsoleService configurationAdmin = new AdminConsoleService(CONFIGURATION_ADMIN, configurationAdminImpl) {
@Override
public boolean isPermittedToViewService(String servicePid) {
return true;
}
};
configurationAdmin.setGuestClaimsHandlerExt(mockGuestClaimsHandlerExt);
Dictionary<String, Object> testProp = new Hashtable<>();
testProp.put(TEST_KEY, TEST_VALUE);
when(testConfig.getPid()).thenReturn(TEST_PID);
when(testConfig.getFactoryPid()).thenReturn(TEST_FACTORY_PID);
when(testConfig.getBundleLocation()).thenReturn(TEST_LOCATION);
when(testConfig.getProperties()).thenReturn(testProp);
Bundle testBundle = mock(Bundle.class);
Dictionary bundleHeaders = mock(Dictionary.class);
MetaTypeInformation testMTI = mock(MetaTypeInformation.class);
ObjectClassDefinition testOCD = mock(ObjectClassDefinition.class);
ServiceReference testRef1 = mock(ServiceReference.class);
ServiceReference[] testServRefs = { testRef1 };
ArrayList<AttributeDefinition> attDefs = new ArrayList<>();
for (int cardinality : CARDINALITIES) {
for (AdminConsoleService.TYPE type : AdminConsoleService.TYPE.values()) {
AttributeDefinition testAttDef = mock(AttributeDefinition.class);
when(testAttDef.getCardinality()).thenReturn(cardinality);
when(testAttDef.getType()).thenReturn(type.getType());
when(testAttDef.getID()).thenReturn(getKey(cardinality, type));
attDefs.add(testAttDef);
}
}
when(testRef1.getProperty(Constants.SERVICE_PID)).thenReturn(TEST_PID);
when(testRef1.getBundle()).thenReturn(testBundle);
when(testBundle.getLocation()).thenReturn(TEST_LOCATION);
when(testBundle.getHeaders(anyString())).thenReturn(bundleHeaders);
when(bundleHeaders.get(Constants.BUNDLE_NAME)).thenReturn(TEST_BUNDLE_NAME);
when(testOCD.getName()).thenReturn(TEST_OCD);
when(testOCD.getAttributeDefinitions(ObjectClassDefinition.ALL)).thenReturn(attDefs.toArray(new AttributeDefinition[attDefs.size()]));
when(testMTI.getFactoryPids()).thenReturn(new String[] { TEST_FACTORY_PID });
when(testMTI.getPids()).thenReturn(new String[] { TEST_PID });
when(testMTI.getObjectClassDefinition(anyString(), anyString())).thenReturn(testOCD);
when(testMTS.getMetaTypeInformation(testBundle)).thenReturn(testMTI);
when(testBundleContext.getBundles()).thenReturn(new Bundle[] { testBundle });
when(CONFIGURATION_ADMIN.listConfigurations(anyString())).thenReturn(new Configuration[] { testConfig });
when(CONFIGURATION_ADMIN.getConfiguration(anyString(), any())).thenReturn(testConfig);
when(testBundleContext.getAllServiceReferences(anyString(), anyString())).thenReturn(testServRefs);
when(testBundleContext.getAllServiceReferences(anyString(), anyString())).thenReturn(testServRefs);
return configurationAdmin;
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class SourceConfigurationHandlerTest method setUp.
@Before
public void setUp() throws Exception {
parser = new XmlParser();
adminService = mock(FederationAdminService.class);
configAdmin = mock(ConfigurationAdmin.class);
metaTypeService = mock(MetaTypeService.class);
bundleContext = mock(BundleContext.class);
executorService = mock(ExecutorService.class);
sch = new SourceConfigurationHandler(adminService, executorService) {
@Override
protected BundleContext getBundleContext() {
return bundleContext;
}
};
sch.setMetacardMarshaller(new MetacardMarshaller(parser));
sch.setConfigurationAdmin(configAdmin);
sch.setMetaTypeService(metaTypeService);
sch.setSlotHelper(new SlotTypeHelper());
sch.setRegistryTypeHelper(new RegistryPackageTypeHelper());
sch.setActivateConfigurations(false);
sch.setPreserveActiveConfigurations(true);
sch.setUrlBindingName("urlBindingName");
sch.setBindingTypeFactoryPid(Collections.singletonList("CSW_2.0.2=Csw_Federated_Source"));
sch.setSourceActivationPriorityOrder(Collections.singletonList("CSW_2.0.2"));
sch.setCleanUpOnDelete(false);
Bundle bundle = mock(Bundle.class);
mti = mock(MetaTypeInformation.class);
ocd = mock(ObjectClassDefinition.class);
config = mock(Configuration.class);
AttributeDefinition adi = new AttributeDefinitionImpl("attId", "attName", "attDesc", "attValue");
when(adminService.getRegistryMetacards()).thenReturn(new ArrayList());
when(bundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
when(configAdmin.listConfigurations("(id=TestRegNode")).thenReturn(null);
when(configAdmin.listConfigurations("(registry-id=urn:uuid:2014ca7f59ac46f495e32b4a67a51276")).thenReturn(null);
when(metaTypeService.getMetaTypeInformation(any(Bundle.class))).thenReturn(mti);
when(mti.getObjectClassDefinition(anyString(), anyString())).thenReturn(ocd);
when(ocd.getAttributeDefinitions(anyInt())).thenReturn(new AttributeDefinition[] { adi });
when(configAdmin.createFactoryConfiguration(anyString(), anyString())).thenReturn(config);
mcard = new MetacardImpl(new RegistryObjectMetacardType());
mcard.setTags(Collections.singleton(RegistryConstants.REGISTRY_TAG));
mcard.setId("2014ca7f59ac46f495e32b4a67a51276");
mcard.setAttribute(RegistryObjectMetacardType.REGISTRY_ID, "urn:uuid:2014ca7f59ac46f495e32b4a67a51276");
mcard.setMetadata(getMetadata("/csw-rim-node-csw-binding.xml"));
mcard.setTitle("TestRegNode");
Dictionary<String, Object> eventProperties = new Hashtable<>();
eventProperties.put("ddf.catalog.event.metacard", mcard);
createEvent = new Event("ddf/catalog/event/CREATED", eventProperties);
updateEvent = new Event("ddf/catalog/event/UPDATED", eventProperties);
deleteEvent = new Event("ddf/catalog/event/DELETED", eventProperties);
System.setProperty(RegistryConstants.REGISTRY_ID_PROPERTY, "myRegId");
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class ConfigurationAdminExt method getObjectClassDefinitions.
/**
* Returns the <code>ObjectClassDefinition</code> objects for the IDs returned by the
* <code>idGetter</code>. Depending on the <code>idGetter</code> implementation this will be for
* factory PIDs or plain PIDs.
*
* @param idGetter The {@link IdGetter} used to get the list of factory PIDs or PIDs from
* <code>MetaTypeInformation</code> objects.
* @return Map of <code>ObjectClassDefinition</code> objects indexed by the PID (or factory PID)
* to which they pertain
*/
private Map getObjectClassDefinitions(final IdGetter idGetter) {
Locale locale = Locale.getDefault();
final Map objectClassesDefinitions = new HashMap();
final MetaTypeService mts = this.getMetaTypeService();
if (mts != null) {
final Bundle[] bundles = this.getBundleContext().getBundles();
for (int i = 0; i < bundles.length; i++) {
final MetaTypeInformation mti = mts.getMetaTypeInformation(bundles[i]);
if (mti != null) {
final String[] idList = idGetter.getIds(mti);
for (int j = 0; idList != null && j < idList.length; j++) {
// After getting the list of PIDs, a configuration might be
// removed. So the getObjectClassDefinition will throw
// an exception, and this will prevent ALL configuration from
// being displayed. By catching it, the configurations will be
// visible
ObjectClassDefinition ocd = null;
try {
ocd = mti.getObjectClassDefinition(idList[j], locale.toString());
} catch (IllegalArgumentException ignore) {
// ignore - just don't show this configuration
}
if (ocd != null) {
objectClassesDefinitions.put(idList[j], ocd);
}
}
}
}
}
return objectClassesDefinitions;
}
Aggregations