Search in sources :

Example 61 with Dictionary

use of java.util.Dictionary 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 62 with Dictionary

use of java.util.Dictionary 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)

Example 63 with Dictionary

use of java.util.Dictionary in project sling by apache.

the class SlingServletResolverTest method setUp.

@Before
public void setUp() throws Exception {
    mockResourceResolver = new MockResourceResolver() {

        @Override
        public void close() {
        // nothing to do;
        }

        @Override
        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            return null;
        }

        @Override
        public ResourceResolver clone(Map<String, Object> authenticationInfo) throws LoginException {
            throw new LoginException("MockResourceResolver can't be cloned - excepted for this test!");
        }

        @Override
        public void refresh() {
        // nothing to do
        }
    };
    mockResourceResolver.setSearchPath("/");
    final ResourceResolverFactory factory = new ResourceResolverFactory() {

        @Override
        public ResourceResolver getAdministrativeResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getServiceResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getThreadResourceResolver() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    servlet = new MockSlingRequestHandlerServlet();
    servletResolver = new SlingServletResolver();
    Class<?> resolverClass = servletResolver.getClass();
    // set resource resolver factory
    final Field resolverField = resolverClass.getDeclaredField("resourceResolverFactory");
    resolverField.setAccessible(true);
    resolverField.set(servletResolver, factory);
    MockBundle bundle = new MockBundle(1L);
    MockBundleContext bundleContext = new MockBundleContext(bundle) {

        @Override
        public ServiceRegistration registerService(String s, Object o, Dictionary dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration registerService(String[] strings, Object o, Dictionary dictionary) {
            return null;
        }
    };
    MockServiceReference serviceReference = new MockServiceReference(bundle);
    serviceReference.setProperty(Constants.SERVICE_ID, 1L);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_NAME, SERVLET_NAME);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_PATHS, SERVLET_PATH);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, SERVLET_EXTENSION);
    servletResolver.bindServlet(SlingServletResolverTest.this.servlet, serviceReference);
    servletResolver.activate(bundleContext, new SlingServletResolver.Config() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return SlingServletResolver.Config.class;
        }

        @Override
        public String servletresolver_servletRoot() {
            return "0";
        }

        @Override
        public String[] servletresolver_paths() {
            return new String[] { "/" };
        }

        @Override
        public String[] servletresolver_defaultExtensions() {
            // TODO Auto-generated method stub
            return new String[] { "html" };
        }

        @Override
        public int servletresolver_cacheSize() {
            return 200;
        }
    });
    String path = "/" + MockSlingHttpServletRequest.RESOURCE_TYPE + "/" + ResourceUtil.getName(MockSlingHttpServletRequest.RESOURCE_TYPE) + ".servlet";
    MockServletResource res = new MockServletResource(mockResourceResolver, servlet, path);
    mockResourceResolver.addResource(res);
    MockResource parent = new MockResource(mockResourceResolver, ResourceUtil.getParent(res.getPath()), "nt:folder");
    mockResourceResolver.addResource(parent);
    List<Resource> childRes = new ArrayList<>();
    childRes.add(res);
    mockResourceResolver.addChildren(parent, childRes);
}
Also used : Dictionary(java.util.Dictionary) MockResource(org.apache.sling.commons.testing.sling.MockResource) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) MockServletResource(org.apache.sling.servlets.resolver.internal.resource.MockServletResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource) MockServletResource(org.apache.sling.servlets.resolver.internal.resource.MockServletResource) Annotation(java.lang.annotation.Annotation) MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) MockBundleContext(org.apache.sling.commons.testing.osgi.MockBundleContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) Map(java.util.Map) Before(org.junit.Before)

Example 64 with Dictionary

use of java.util.Dictionary in project sling by apache.

the class DefaultXingOauthUserManager method configure.

protected synchronized void configure(final ComponentContext componentContext) {
    final Dictionary properties = componentContext.getProperties();
    autoCreateUser = PropertiesUtil.toBoolean(properties.get(AUTO_CREATE_USER_PARAMETER), DEFAULT_AUTO_CREATE_USER);
    autoUpdateUser = PropertiesUtil.toBoolean(properties.get(AUTO_UPDATE_USER_PARAMETER), DEFAULT_AUTO_UPDATE_USER);
}
Also used : Dictionary(java.util.Dictionary)

Example 65 with Dictionary

use of java.util.Dictionary in project sling by apache.

the class DefaultXingLoginUserManager method configure.

protected synchronized void configure(final ComponentContext componentContext) {
    final Dictionary properties = componentContext.getProperties();
    secretKey = PropertiesUtil.toString(properties.get(SECRET_KEY_PARAMETER), "").trim();
    userDataProperty = PropertiesUtil.toString(properties.get(USER_DATA_PROPERTY_PARAMETER), DEFAULT_USER_DATA_PROPERTY).trim();
    userHashProperty = PropertiesUtil.toString(properties.get(USER_HASH_PROPERTY_PARAMETER), DEFAULT_USER_HASH_PROPERTY).trim();
    autoCreateUser = PropertiesUtil.toBoolean(properties.get(AUTO_CREATE_USER_PARAMETER), DEFAULT_AUTO_CREATE_USER);
    autoUpdateUser = PropertiesUtil.toBoolean(properties.get(AUTO_UPDATE_USER_PARAMETER), DEFAULT_AUTO_UPDATE_USER);
    if (StringUtils.isEmpty(secretKey)) {
        logger.warn("configured secret key is empty");
    }
}
Also used : Dictionary(java.util.Dictionary)

Aggregations

Dictionary (java.util.Dictionary)198 Hashtable (java.util.Hashtable)91 Test (org.junit.Test)48 Configuration (org.osgi.service.cm.Configuration)33 BundleDescription (org.eclipse.osgi.service.resolver.BundleDescription)28 State (org.eclipse.osgi.service.resolver.State)28 Enumeration (java.util.Enumeration)27 Properties (java.util.Properties)27 BundleContext (org.osgi.framework.BundleContext)24 ArrayList (java.util.ArrayList)22 ServiceReference (org.osgi.framework.ServiceReference)21 HashMap (java.util.HashMap)20 ServiceRegistration (org.osgi.framework.ServiceRegistration)20 IOException (java.io.IOException)17 Map (java.util.Map)17 Bundle (org.osgi.framework.Bundle)16 LinkedHashMap (java.util.LinkedHashMap)13 List (java.util.List)13 Matchers.anyString (org.mockito.Matchers.anyString)11 MinionIdentity (org.opennms.minion.core.api.MinionIdentity)11