Search in sources :

Example 1 with TYPE

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;
}
Also used : Dictionary(java.util.Dictionary) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) Mockito.anyString(org.mockito.Mockito.anyString) ServiceReference(org.osgi.framework.ServiceReference) TYPE(org.codice.ddf.ui.admin.api.ConfigurationAdmin.TYPE) BundleContext(org.osgi.framework.BundleContext) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 2 with TYPE

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));
}
Also used : BigInteger(java.math.BigInteger) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) Mockito.anyString(org.mockito.Mockito.anyString) TYPE(org.codice.ddf.ui.admin.api.ConfigurationAdmin.TYPE) Test(org.junit.Test)

Aggregations

Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 TYPE (org.codice.ddf.ui.admin.api.ConfigurationAdmin.TYPE)2 Mockito.anyString (org.mockito.Mockito.anyString)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 ServiceReference (org.osgi.framework.ServiceReference)1 AttributeDefinition (org.osgi.service.metatype.AttributeDefinition)1 MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)1 MetaTypeService (org.osgi.service.metatype.MetaTypeService)1 ObjectClassDefinition (org.osgi.service.metatype.ObjectClassDefinition)1