Search in sources :

Example 11 with ConfigurationManager

use of org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager in project carbon-apimgt by wso2.

the class TenantServiceCreatorTestCase method testCreatedConfigurationContext.

@Test
public void testCreatedConfigurationContext() throws Exception {
    TenantServiceCreator tenantServiceCreator = new TenantServiceCreator();
    ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
    // Failed to create Tenant's synapse sequences Error
    PowerMockito.mockStatic(Cache.class);
    Cache cache = Mockito.mock(Cache.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(APIManagerConfigurationService.class);
    PowerMockito.mockStatic(CacheProvider.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
    PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
    Mockito.when(CacheProvider.getGatewayKeyCache()).thenReturn(cache);
    Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
    Mockito.when(CacheProvider.getGatewayTokenCache()).thenReturn(cache);
    Mockito.when(CacheProvider.getInvalidTokenCache()).thenReturn(cache);
    tenantServiceCreator.createdConfigurationContext(configurationContext);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(FileUtils.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn("abc.com");
    AxisConfiguration axisConfiguration = Mockito.mock(AxisConfiguration.class);
    Mockito.when(configurationContext.getAxisConfiguration()).thenReturn(axisConfiguration);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    URL url = new URL("http", "localhost", 5000, "/fle/");
    Mockito.when(axisConfiguration.getRepository()).thenReturn(url);
    File tenantAxis2Repo = Mockito.mock(File.class);
    File synapseConfigsDir = Mockito.mock(File.class);
    // Couldn't create the synapse-config root on the file system error is logged.
    tenantServiceCreator.createdConfigurationContext(configurationContext);
    PowerMockito.whenNew(File.class).withArguments("/file/").thenReturn(tenantAxis2Repo);
    PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(synapseConfigsDir);
    Mockito.when(synapseConfigsDir.mkdir()).thenReturn(true);
    String synapseConfigsDirLocation = "/file/synapse-confgs";
    Mockito.when(synapseConfigsDir.getAbsolutePath()).thenReturn(synapseConfigsDirLocation);
    Mockito.doNothing().when(axisConfiguration).addParameter(SynapseConstants.Axis2Param.SYNAPSE_CONFIG_LOCATION, synapseConfigsDirLocation);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
    Mockito.when(privilegedCarbonContext.getRegistry(RegistryType.SYSTEM_CONFIGURATION)).thenReturn(userRegistry);
    PowerMockito.whenNew(ConfigurationManager.class).withArguments(userRegistry, configurationContext).thenReturn(configurationManager);
    ConfigurationTracker tracker = Mockito.mock(ConfigurationTracker.class);
    Mockito.when(configurationManager.getTracker()).thenReturn(tracker);
    Mockito.when(tracker.getCurrentConfigurationName()).thenReturn("config-name");
    Mockito.when(synapseConfigsDir.exists()).thenReturn(false, false, false, true);
    copyFile("/repository/resources/apim-synapse-config/main.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "main.xml");
    copyFile("/repository/resources/apim-synapse-config/fault.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "falut.xml");
    copyFile("/repository/resources/apim-synapse-config/_auth_failure_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_auth_failure_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_resource_mismatch_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_resource_mismatch_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_throttle_out_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_throttle_out_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_sandbox_key_error_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_sandbox_key_error_.xml");
    copyFile("/repository/resources/apim-synapse-config/_production_key_error_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_production_key_error_.xml");
    copyFile("/repository/resources/apim-synapse-config/_cors_request_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_cors_request_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_threat_fault.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_threat_fault.xml");
    // test IOException Error while copying API manager specific synapse sequences
    tenantServiceCreator.createdConfigurationContext(configurationContext);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ConfigurationTracker(org.wso2.carbon.mediation.initializer.configurations.ConfigurationTracker) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) CacheProvider(org.wso2.carbon.apimgt.impl.caching.CacheProvider) URL(java.net.URL) File(java.io.File) ConfigurationManager(org.wso2.carbon.mediation.initializer.configurations.ConfigurationManager) Cache(javax.cache.Cache) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with ConfigurationManager

use of org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager in project carbon-identity-framework by wso2.

the class ConfigStoreBasedTemplateHandler method updateTemplateById.

@Override
public void updateTemplateById(String templateId, Template template) throws TemplateManagementException {
    ConfigurationManager configManager = TemplateManagerDataHolder.getInstance().getConfigurationManager();
    validateWSTrustTemplateAvailability(templateId);
    try {
        configManager.replaceResource(new TemplateToResource().apply(template));
    } catch (ConfigurationManagementException e) {
        if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_ID_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
            throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND, e, templateId, getTenantDomainFromCarbonContext());
        } else if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_ALREADY_EXISTS.getCode().equals(e.getErrorCode())) {
            throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_ALREADY_EXIST, e, templateId, getTenantDomainFromCarbonContext());
        }
        throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_TEMPLATE, e, templateId, getTenantDomainFromCarbonContext());
    }
}
Also used : TemplateToResource(org.wso2.carbon.identity.template.mgt.function.TemplateToResource) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)

Example 13 with ConfigurationManager

use of org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager in project carbon-identity-framework by wso2.

the class ConfigStoreBasedTemplateHandler method deleteTemplateById.

@Override
public void deleteTemplateById(String templateId) throws TemplateManagementException {
    ConfigurationManager configManager = TemplateManagerDataHolder.getInstance().getConfigurationManager();
    validateWSTrustTemplateAvailability(templateId);
    try {
        configManager.deleteResourceById(templateId);
    } catch (ConfigurationManagementException e) {
        if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_ID_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
            throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND, e, templateId, getTenantDomainFromCarbonContext());
        }
        throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_DELETE_TEMPLATE_BY_ID, e, templateId, getTenantDomainFromCarbonContext());
    }
}
Also used : ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)

Example 14 with ConfigurationManager

use of org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager in project carbon-identity-framework by wso2.

the class ConfigStoreBasedTemplateHandler method getTemplateById.

@Override
public Template getTemplateById(String templateId) throws TemplateManagementException {
    ConfigurationManager configManager = TemplateManagerDataHolder.getInstance().getConfigurationManager();
    validateWSTrustTemplateAvailability(templateId);
    try {
        Resource resource = configManager.getTenantResourceById(templateId);
        Template template = new ResourceToTemplate().apply(resource);
        if (resource.getFiles().size() == 1) {
            try (InputStream templateScriptInputStream = configManager.getFileById(resource.getResourceType(), resource.getResourceName(), resource.getFiles().get(0).getId())) {
                template.setTemplateScript(IOUtils.toString(templateScriptInputStream));
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(template.getTemplateType().toString() + " can have only one templated object. But the " + "template with id: " + templateId + " has " + resource.getFiles().size() + " templated " + "object/s. Therefore templated object is not retrieved.");
            }
        }
        return template;
    } catch (ConfigurationManagementException e) {
        if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_ID_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
            throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND, e, templateId, getTenantDomainFromCarbonContext());
        }
        throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_RETRIEVE_TEMPLATE_BY_ID, e, templateId, getTenantDomainFromCarbonContext());
    } catch (IOException e) {
        throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_RETRIEVE_TEMPLATE_BY_ID, e, templateId, getTenantDomainFromCarbonContext());
    }
}
Also used : ResourceToTemplate(org.wso2.carbon.identity.template.mgt.function.ResourceToTemplate) InputStream(java.io.InputStream) Resource(org.wso2.carbon.identity.configuration.mgt.core.model.Resource) TemplateToResource(org.wso2.carbon.identity.template.mgt.function.TemplateToResource) IOException(java.io.IOException) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager) ResourceToTemplate(org.wso2.carbon.identity.template.mgt.function.ResourceToTemplate) Template(org.wso2.carbon.identity.template.mgt.model.Template)

Example 15 with ConfigurationManager

use of org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager in project carbon-identity-framework by wso2.

the class ConfigurationManagerTest method prepareConfigs.

private void prepareConfigs() throws Exception {
    // Mock get maximum query length call.
    mockStatic(IdentityUtil.class);
    when(IdentityUtil.getProperty(any(String.class))).thenReturn("4194304");
    when(IdentityUtil.getEndpointURIPath(any(String.class), anyBoolean(), anyBoolean())).thenReturn("/t/bob.com/api/identity/config-mgt/v1.0/resource/file/publisher/SMSPublisher/9e038218-8e99-4dae-bf83-a78f5dcd73a8");
    ConfigurationManagerComponentDataHolder.setUseCreatedTime(true);
    ConfigurationManagerConfigurationHolder configurationHolder = new ConfigurationManagerConfigurationHolder();
    ConfigurationDAO configurationDAO = new ConfigurationDAOImpl();
    configurationHolder.setConfigurationDAOS(Collections.singletonList(configurationDAO));
    mockCarbonContextForTenant(SUPER_TENANT_ID, SUPER_TENANT_DOMAIN_NAME);
    mockIdentityTenantUtility();
    configurationManager = new ConfigurationManagerImpl(configurationHolder);
}
Also used : ConfigurationDAO(org.wso2.carbon.identity.configuration.mgt.core.dao.ConfigurationDAO) ConfigurationDAOImpl(org.wso2.carbon.identity.configuration.mgt.core.dao.impl.ConfigurationDAOImpl) ConfigurationManagerConfigurationHolder(org.wso2.carbon.identity.configuration.mgt.core.model.ConfigurationManagerConfigurationHolder)

Aggregations

ConfigurationManager (org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)8 File (java.io.File)5 IOException (java.io.IOException)5 ConfigurationManagementException (org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException)5 Resource (org.wso2.carbon.identity.configuration.mgt.core.model.Resource)4 TemplateToResource (org.wso2.carbon.identity.template.mgt.function.TemplateToResource)4 ConfigurationManager (org.wso2.carbon.mediation.initializer.configurations.ConfigurationManager)4 InputStream (java.io.InputStream)3 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)3 ConfigurationDAO (org.wso2.carbon.identity.configuration.mgt.core.dao.ConfigurationDAO)3 ConfigurationDAOImpl (org.wso2.carbon.identity.configuration.mgt.core.dao.impl.ConfigurationDAOImpl)3 ConfigurationManagerConfigurationHolder (org.wso2.carbon.identity.configuration.mgt.core.model.ConfigurationManagerConfigurationHolder)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 ServerContextInformation (org.apache.synapse.ServerContextInformation)2 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 Activate (org.osgi.service.component.annotations.Activate)2