use of org.codice.ddf.ui.admin.api.ConfigurationAdmin.TYPE in project ddf by codice.
the class ConfigurationAdminTest method getConfigAdmin.
private ConfigurationAdmin getConfigAdmin() throws IOException, InvalidSyntaxException {
final BundleContext testBundleContext = mock(BundleContext.class);
final MetaTypeService testMTS = mock(MetaTypeService.class);
ConfigurationAdminExt configurationAdminExt = new ConfigurationAdminExt(CONFIGURATION_ADMIN) {
@Override
BundleContext getBundleContext() {
return testBundleContext;
}
@Override
MetaTypeService getMetaTypeService() {
return testMTS;
}
@Override
public boolean isPermittedToViewService(String servicePid) {
return true;
}
};
ConfigurationAdmin configurationAdmin = new ConfigurationAdmin(CONFIGURATION_ADMIN, configurationAdminExt);
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 (TYPE type : 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.getBundle()).thenReturn(testBundle);
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(), anyString())).thenReturn(testConfig);
when(testBundleContext.getAllServiceReferences(anyString(), anyString())).thenReturn(testServRefs);
when(testBundleContext.getAllServiceReferences(anyString(), anyString())).thenReturn(testServRefs);
return configurationAdmin;
}
use of org.codice.ddf.ui.admin.api.ConfigurationAdmin.TYPE in project ddf by codice.
the class ConfigurationAdminTest method testUpdate.
/**
* Tests the {@link ConfigurationAdmin#update(String, Map)} and
* {@link ConfigurationAdmin#updateForLocation(String, String, Map)} methods
*
* @throws Exception
*/
@Test
public void testUpdate() throws Exception {
ConfigurationAdmin configAdmin = getConfigAdmin();
// test every typed cardinality<->cardinality mapping
for (int i = 0; i < CARDINALITIES.length; i++) {
int cardinality = CARDINALITIES[i];
Map<String, Object> testConfigTable = new Hashtable<>();
for (TYPE type : TYPE.values()) {
for (int keyCardinality : CARDINALITIES) {
testConfigTable.put(getKey(keyCardinality, type), getValue(cardinality, type));
}
}
configAdmin.update(TEST_PID, testConfigTable);
verify(testConfig, times(i + 1)).update(any(Dictionary.class));
}
Hashtable<String, Object> values = new Hashtable<>();
String arrayString = getKey(CARDINALITY_ARRAY, TYPE.STRING);
ArgumentCaptor<Dictionary> captor = ArgumentCaptor.forClass(Dictionary.class);
// test string jsonarray parsing
values.put(arrayString, "[\"foo\",\"bar\",\"baz\"]");
String primitiveBoolean = getKey(CARDINALITY_PRIMITIVE, TYPE.BOOLEAN);
// test string valueof parsing
values.put(primitiveBoolean, "true");
String primitiveInteger = getKey(CARDINALITY_PRIMITIVE, TYPE.INTEGER);
// test string valueof parsing for non-strings
values.put(primitiveInteger, (long) TEST_INT);
String arrayInteger = getKey(CARDINALITY_ARRAY, TYPE.INTEGER);
// test empty array substitution
values.put(arrayInteger, "");
configAdmin.update(TEST_PID, values);
verify(testConfig, times(4)).update(captor.capture());
assertThat(((String[]) captor.getValue().get(arrayString)).length, equalTo(3));
assertThat((Boolean) captor.getValue().get(primitiveBoolean), equalTo(true));
assertThat((int) captor.getValue().get(primitiveInteger), equalTo(TEST_INT));
assertThat(((Integer[]) captor.getValue().get(arrayInteger)).length, equalTo(0));
}
Aggregations